import {getStore, setStore, Decrypt,Encrypt,inputString} from '../libs/util'
import axios from 'axios'
import config from './config'
import store from '../vuex'
import {SET_MSG,SET_TOKEN,LOGOUT}from '../vuex/types'
import router from '../router'
import {API_ROOT, CPT_ROOT} from '../config/config'


let ajax = axios.create(config)
let ajaxCpt = axios.create(config)

ajaxCpt.defaults.baseURL = CPT_ROOT //无验证地址
ajax.defaults.baseURL = API_ROOT //需要token验证地址

// http request 拦截器，需要验证的地址请求加token
ajax.interceptors.request.use(config => {

  if (getStore('token')) {
    config.headers.Authorization = getStore('token');
  }
  return config
}, error => {
  console.log(error)
  // console.log(error.response.status)
  // console.log(error.response.headers)
  // console.log('Error',error.message)
  store.commit(LOGOUT);
  router.replace('/login');
  store.commit(SET_MSG,"网络或服务器故障！")
  return Promise.reject(error)
})

// http request 拦截器
ajaxCpt.interceptors.request.use(config => {
  if (getStore('token')) {
    config.headers.Authorization = getStore('token');
  }
  return config
}, err => {

  // console.log(JSON.stringify(error));//console : Error: Request failed with status code 402
  return Promise.reject(err)
})
// http response 拦截器,解密服务器传输Jdata数据为Data
ajax.interceptors.response.use(response => {
    store.commit(SET_MSG,response.data.message)
    if(response.data.token.length>0){
      store.commit(SET_TOKEN,response.data.token)
    }
    let aeskey = getStore('token').substr(5, 16)
    if(!response.data.jdata) {
      return  response;
    }
    let ciphertext = response.data.jdata
    let tsu = Decrypt(ciphertext, aeskey)
    setStore('token',response.data.token)
    response.data.Data=tsu
    // console.log("tsu:", response.data)
    return response;
  },
  error => {
    store.commit(SET_MSG,"请检查网络或联系系统管理员！")
    store.commit(LOGOUT);
    router.replace('/login');
    return Promise.reject(error.response.data)
  });

// 加密post数据
export function post(url, params) {
  let aeskey = getStore('token').substr(5, 16)
  params=inputString(params)
  let tsu ={
    tdata:Encrypt(params, aeskey)}
  return new Promise((resolve, reject) => {
    ajax.post(url, tsu)
      .then(response => {
        resolve(response);
      },   error => {
        return reject(error)
      })
      .catch((error) => {
        return reject(error)
      })
  })
}

export default {
  // 登录
  login: function (data) {
    return ajaxCpt.post('/Loginyz', data,)
  },
  // 获取验证码
  getCpt: function () {
    return ajaxCpt.get('/Getcpt', {},)
  },
  // 退出
  logout: function () {
    return ajax.get('/auth/LogoutAPI', {},)
  },
  //获取用户数据
  getMe: function () {
    return ajax.get('/user/Getme', {},)
  },
  //资源类
  getResource: function () {
    return ajax.get('/resource/', {},)
  },
  updateResource: function (data) {
    return post('/resource/Update', data,)
  },
  addResource: function (data) {
    return post('/resource/', data,)
  },
  deleteResource: function (data) {
    return post('/resource/Delete', data,)
  },
  // 用户类
  getUser: function () {
    return ajax.get('/user/', {},)
  },
  GetAllOrgUsers: function () {
    return ajax.get('/user/GetAllOrgUsers', {},)
  },
  updateUser: function (data) {
    return post('/user/Update', data,)
  },
  addUser: function (data) {
    return post('/user/', data,)
  },
  deleteUser: function (data) {
    return post('/user/Delete', data,)
  },
  deleteUsers: function (data) {
    return post('/user/DeleteUsers', data,)
  },
  DeleteDepts: function (data) {
    return post('/user/DeleteDepts', data,)
  },
  ChangePwd: function (data) {
    return post('/user/ChangePwd', data,)
  },
  ResetPwd: function (data) {
    return post('/user/ResetPwd', data,)
  },
  UpdateUserProf: function (data) {
    return post('/userprof/Update', data,)
  },
  PhotoCancel:function (data) {
    return post('/userprof/PhotoCancel', data,)
  },
  //客户单位类
  getOrganization: function () {
    return ajax.get('/organization/', {},)
  },
  getOrganizationList: function () {
    return ajax.get('/organization/GetOrganizationList', {},)
  },
  updateOrganization: function (data) {
    return post('/organization/Update', data,)
  },
  addOrganization: function (data) {
    return post('/organization/', data,)
  },
  deleteOrganization: function (data) {
    return post('/organization/Delete', data,)
  },
  //角色
  getRole: function () {
    return ajax.get('/role/', {},)
  },
  getRoleList: function () {
    return ajax.get('/role/GetRoleList', {},)
  },
  GetOrgRoleList:function () {
    return ajax.get('/role/GetOrgRoleList', {},)
  },
  updateRole: function (data) {
    return post('/role/Update', data,)
  },
  addRole: function (data) {
    return post('/role/', data,)
  },
  deleteRole: function (data) {
    return post('/role/Delete', data,)
  },
  deleteRoles: function (data) {
    return post('/role/DeleteRoles', data,)
  },
  GetRoleResourceTree: function (data) {
    return ajax.get('/role/GetRoleResourceTree/'+data, {},)
  },
  AssignResources: function (data) {
    let aeskey = getStore('token').substr(5, 16)
    let tsu ={
      tdata:Encrypt(data, aeskey)}
    return ajax.post('/role/AssignResources', tsu,)
  },
  // 部门
  GetDept: function () {
    return ajax.get('/dept/', {},)
  },
  UpdateDept: function (data) {
    return post('/dept/Update', data,)
  },
  AddDept: function (data) {
    return post('/dept/', data,)
  },
  DeleteDept: function (data) {
    return post('/dept/Delete', data,)
  },
  AssignUserDept: function (data) {
    let aeskey = getStore('token').substr(5, 16)
    let tsu ={
      tdata:Encrypt(data, aeskey)}
    return ajax.post('/dept/AssignUserDept', tsu,)
  },
  //日志
  getLog: function () {
    return ajax.get('/log/', {},)
  },
  deleteLogs: function (data) {
    return post('/log/DeleteLogs', data,)
  },
// 公告
  getBulletin: function (data) {
    return post('/bulletin/GetAll', data,)
  },
  getMyBulletin: function () {
    return ajax.get('/bulletin/GetVaildateBulletins', {},)
  },
  updateBulletin: function (data) {
    return post('/bulletin/Update', data,)
  },
  addBulletin: function (data) {
    return post('/bulletin/', data,)
  },
  deleteBulletin: function (data) {
    return post('/bulletin/Delete', data,)
  },
  deleteBulletins: function (data) {
    return post('/bulletin/DeleteBulletins', data,)
  },
  // 直播频道
  getLiveChannel: function (data) {
    return post('/livechannel/GetAll', data,)
  },
  updateLiveChannel: function (data) {
    return post('/livechannel/Update', data,)
  },
  addLiveChannel: function (data) {
    return post('/livechannel/', data,)
  },
  deleteLiveChannel: function (data) {
    return post('/livechannel/Delete', data,)
  },
  deleteLiveChannels: function (data) {
    return post('/livechannel/DeleteLiveChannels', data,)
  },
  GetLiveList:function () {
    return ajax.get('/livechannel/GetLiveList', {},)
  },
  GetLiveUserByLive:function (data) {
    return ajax.get('/livechannel/GetLiveUserByLive/'+data, {},)
  },
  // 点播频道
  getVideo: function (data) {
    return post('/video/GetAll', data,)
  },
  updateVideo: function (data) {
    return post('/video/Update', data,)
  },
  addVideo: function (data) {
    return post('/video/', data,)
  },
  deleteVideo: function (data) {
    return post('/video/Delete', data,)
  },
  deleteVideos: function (data) {
    return post('/video/DeleteVideos', data,)
  },
  VideoCancel: function (data) {
    return post('/video/VideoCancel', data,)
  },
  GetVideoList: function () {
    return  ajax.get('/video/GetVideoList', {},)
  },
  GetVideoUserByVideo:function (data) {
    return ajax.get('/video/GetVideoUserByVideo/'+data, {},)
  },
//  观看行为
  VideoWatch: function (data) {
    return post('/video/VideoWatch', data,)
  },
  watchhistory: function (data) {
    return post('/watchhistory/', data,)
  },
  GetWatchHistory: function () {
    return  ajax.get('/watchhistory/GetWatchHistory', {},)
  },
  // 统计类
  StatisticsOrgs: function () {
    return  ajax.get('/statistics/StatisticsOrgs', {},)
  },
  StatisticsUser: function () {
    return  ajax.get('/statistics/StatisticsUser', {},)
  },
  StatisticsVideo: function () {
    return  ajax.get('/statistics/StatisticsVideo', {},)
  },
  StatisticsLive: function () {
    return  ajax.get('/statistics/StatisticsLive', {},)
  },
  StatisticsStore: function () {
    return  ajax.get('/statistics/StatisticsStore', {},)
  },
}
